package group_1.MultiSnake;

import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

/**
 * Represents the head of a snake
 * 
 */
public class Head extends Sprite implements SnakeConstants{
	private Rectangle collisionSurface;		// The surface of the head which is used to determine collision
	private float headCollisionWidthDiff;	// The difference in width between head and collision surface
	private float headCollisionLengthDiff; 	// The difference in length between head and collision surface
	private int currDirection;				// The current direction which the head is moving
	
	/**
	 * Constructor - construct a new head object
	 * 
	 * @param pX The start x-position of the head
	 * @param pY The start y-position of the head
	 * @param collisionWidth The width of the collision surface
	 * @param collisionLength The length of the collision surface
	 * @param startDirection The start direction of the head
	 * @param pTextureRegion The image representing the head
	 */
	public Head(float pX, float pY, float collisionWidth, float collisionLength, int startDirection, TextureRegion pTextureRegion) {
		super(pX, pY, HEAD_LENGTH, HEAD_WIDTH, pTextureRegion);
		collisionSurface = new Rectangle(pX, pY, collisionWidth, collisionLength);
		headCollisionWidthDiff = (HEAD_WIDTH-collisionWidth)/2;
		headCollisionLengthDiff = (HEAD_LENGTH-collisionLength)/2;
		currDirection = startDirection;
		setPosition(pX, pY);
	}
	
	/**
	 * Get the collision surface of the head
	 * 
	 * @return Rectangle representing the collision surface of the head
	 */
	public Rectangle getCollisionSurface() {
		return collisionSurface;
	}
	
	/**
	 * Sets the direction that the head should move and rotates the image
	 * representing the head
	 * 
	 * @param newDirection The new direction of the head
	 */
	public void setDirection(int newDirection) {
		switch(newDirection) {
		case RIGHT:
			setRotation(0);
			break;
		case DOWN:
			setRotation(90);
			break;
		case LEFT:
			setRotation(180);
			break;
		case UP:
			setRotation(270);
			break;
		}
		currDirection = newDirection;
	}
	
	/**
	 * Move the head a given step in its current direction
	 * 
	 * @param step The distance to move the head
	 */
	public void move(float step) {
		switch(currDirection) {
		case UP:
			this.setPosition(this.getX(), this.getY()-step);
			break;
		case RIGHT:
			this.setPosition(this.getX()+step, this.getY());
			break;
		case DOWN:
			this.setPosition(this.getX(), this.getY()+step);
			break;
		case LEFT:
			this.setPosition(this.getX()-step, this.getY());
		}
	}
	
	/**
	 * Set the position of the head.
	 * Reposition the collisionsurface depending on which direction the head is moving
	 */
	@Override
	public void setPosition(float x, float y) {
		super.setPosition(x, y);
		
		switch(currDirection) {
		case UP:
			collisionSurface.setPosition(this.getX()+headCollisionWidthDiff, this.getY()+headCollisionLengthDiff);
			break;
		case RIGHT:
			collisionSurface.setPosition(this.getX(), this.getY()+headCollisionWidthDiff);
			break;
		case DOWN:
			collisionSurface.setPosition(this.getX()+headCollisionWidthDiff, this.getY());
			break;
		case LEFT:
			collisionSurface.setPosition(this.getX()+headCollisionLengthDiff, this.getY()+headCollisionWidthDiff);
		}
	}
}
